package com.zy.mc_module.utils.retrofit.disposable;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created on 18/10/23
 * 订阅管理类
 */
public class SubscriptionManager implements SubscriptionHelper<Object> {
    public static SubscriptionManager manager;
    private CompositeDisposable mDisposable;

    public SubscriptionManager() {
        if (mDisposable == null) {
            mDisposable = new CompositeDisposable();
        }
    }

    @Override
    public void add(Disposable disposable) {
        if (disposable != null) {
            mDisposable.add(disposable);
        }
    }

    @Override
    public void cancel(Disposable disposable) {
        if (mDisposable != null) {
            mDisposable.delete(disposable);
        }
    }

    @Override
    public void cancelall() {
        if (mDisposable != null) {
            mDisposable.clear();
        }
    }

    public static SubscriptionManager getInstance() {
        if (manager == null) {
            synchronized (SubscriptionManager.class) {
                if (manager == null) {
                    manager = new SubscriptionManager();
                }
            }
        }
        return manager;
    }
}
